package com.turing.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.turing.system.entity.Admin;
import com.turing.system.feign.AuthService;
import com.turing.system.service.AdminService;
import enums.ErrorCode;
import org.springframework.web.bind.annotation.*;
import result.Result;
import utils.TokenUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Arrays;

/**
 * @author: shihonggang
 * @Date: 2023/4/8 20:38
 * @Description: 管理员控制器
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Resource
    private AdminService adminService;
    @Resource
    private HttpServletRequest request;
    @Resource
    private AuthService authService;

    /**
     * @author: shihonggang
     * @Date: 2023/4/8 20:53
     * @Description: 根据账户名查询账户
     */
    @GetMapping("getAdminByLoginAccount")
    public Result getAdminByLoginAccount(@RequestParam("loginAccount") String loginAccount) {
        if (StringUtils.isNullOrEmpty(loginAccount)) {
            return Result.fail(ErrorCode.ERROR_PARAM_REQUIRED_MISSING);
        }
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getLoginAccount, loginAccount)
                .last("limit 1");
        Admin admin = adminService.getOne(wrapper);
        return Result.success(admin);
    }

    @GetMapping("getAdminByToken")
    public Result getAdminByToken() {
        String token = TokenUtil.getTokenByAuthorization(request.getHeader("Authorization"), request.getHeader("token"));
        Object user = authService.getUserInfoByToken(token).getData();
        System.out.println(user);
        return Result.success(user);
    }

    /**
     * 51101管理员列表(PC)
     * @param account
     * @param rankId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("query")
    public Result query(
            @RequestParam(value = "account", required = false) String account,
            @RequestParam(value = "rankId", required = false) String rankId,
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        System.out.println("account = " + account);
        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        return adminService.query(account, rankId, pageNum, pageSize);
    }


    /**
     * 51102管理员详情(PC)
     *
     * @param id
     * @return
     */
    @GetMapping("getById")
    public Result getById(@RequestParam(value = "id", required = true) Integer id) {

        if (id==null){
            return Result.fail(0,"用户不存在");
        }
        return adminService.getDetail(id);
    }

    /**
     * 51103增加管理员(PC)
     *
     * @param jsonObject
     * @return
     * @throws ParseException
     */
    @PostMapping("addSave")
    public Result addSave(@RequestBody JSONObject jsonObject) throws ParseException {

        return adminService.addSave(jsonObject);
    }

    /**
     * 修改管理员
     * @param jsonObject
     * @return
     * @throws ParseException
     */
    @PatchMapping("editSave")
    public Result editSave(@RequestBody JSONObject jsonObject) throws ParseException {
        return adminService.editSave(jsonObject);
    }

    /**
     * 删除管理员（多条）
     * @param ids
     * @return
     */
    @DeleteMapping("deleteByIds")
    public Result deleteByIds(@RequestParam(value = "ids",required = true) Integer[] ids) {
        System.out.println("ids = " + Arrays.toString(ids));
        return adminService.deleteByIds(ids);
    }


}
